<?php
namespace App\Controller;

use Common\Controller\HomebaseController;
use Think\Controller;
use Think\MultiThread;

class JdController extends Controller
{
    function model_thread_result_get($urls_array)
    {
        foreach($urls_array as $key =>$value)
        {
            $thread_array[$key] =new MultiThread($value['url']);
            $thread_array[$key]->start();
        }
        
        foreach($thread_array as $thread_array_key => $thread_array_value)
        {
            while($thread_array[$thread_array_key]->isRunning())
            {
                usleep(10);
            }
            if($thread_array[$thread_array_key]->join())
            {
                $content_data=$thread_array[$thread_array_key]->data;
                $con_v=$content_data['detail']['data'];
                $content = "";
                if(!empty($con_v)){
                    foreach($con_v['description'] as $desc){
                        if($desc['type'] == 1){//文字或者为空
                            $content .= "<p>" . $desc['content'] . "</p>";
                        }
                        elseif($desc['type'] == 2){//图片
                            $content .= "<p><img src='http:" . $con_v['imghost'] . $desc['content'] . "'></p>";
                        }
                        elseif($desc['type'] == 3){//购买框
                            $content .= '<p><span class="outer_link_insert_box clearfix"><span class="l_box f_left"><span class="price">￥' . $desc['content']['price'] . '</span><span class="hot">' . rand(1000, 9999) . '人喜欢</span></span><a href="/go/' . encode_url("https://item.jd.com/" . $desc['content']['params']['skuId'] . "html") . '" class="btn_outer_link f_right" target="_blank">京东购买</a><span class="del_card">×</span></span>
</p>';
                        }
                        elseif($desc['type'] == 7){//视频
                            $content .= "<iframe src='https:" .$desc['content']['videoUrl'] . "' style='width: 750px;height: 422px;'></iframe>";
                        }
                        continue;
                    }
                    $int=strpos($con_v['indexImage'],"!q");
                    $con_v['indexImage']=substr($con_v['indexImage'], 0,$int);
                    echo $con_v['id']."\n";
                    $variable_data[$thread_array_key] = array(
                        'pic'     => "http:" . $con_v['imghost'] . $con_v['indexImage'],
                        'title'   => $con_v['title'],
                        'content' => $content,
                        'url'     => "https://ai.jd.com/index_new.php?app=Discovergoods&action=getInfoDetail&id={$con_v['id']}",
                        'status'  => 3,
                        'ctime'   => substr($con_v['publishTime'], 0, 10),
                    );
                }
            }
            
        }
        return $variable_data;
    }
    
    //获取详情
    public function jd_pa()
    {
        ini_set('memory_limit', '2048M');
        $urls_array=M('url')->where(array("status"=>0))->order('id asc')->select();
        $t = microtime(true);
        $result = $this->model_thread_result_get($urls_array);
        $num=ceil(count($result)/20);
        for($i=0;$i<$num;$i++){
            $office=$i*20;
            $dataAll=array_slice($result, $office,20);
            M()->execute("update ios_url set `status`=1");
            M("url_crawling")->addAll($dataAll);
        }
        $e = microtime(true);
        echo "多线程：".($e-$t)."\n";
        echo count($result);
    }
    
    public function jd_test()
    {
        $start= microtime(true);
        $data=$this->html_data("https://ai.jd.com/index_new.php?app=Discovergoods&action=getInfoDetail&id=717312");
        $end= microtime(true);
        echo  "content:".round(($end-$start),4)."\n";
        var_dump($data);
    }
    //测试
    public function get_url_test(){
        set_time_limit(0);
        ini_set('memory_limit', '2048M');
        //分类
        $tagtype = 6;
        for($i = 1; $i <= 100; $i++){//获取给分类列表链接
            $url = "https://ai.jd.com/index_new.php?app=Discovergoods&action=getRecommendList&page={$i}&type={$tagtype}";
            $data = $this->html_data($url);
            if( ! empty($data['list'])){
                $j = 0;
                $dataAll = array();
                $start1= microtime(true);
                foreach($data['list'] as $k => $v){//循环类别信息
                    //获取详情链接接口
                    $detail_url = "https://ai.jd.com/index_new.php?app=Discovergoods&action=getInfoDetail&id={$v['articleId']}";
                    if(M('Url')->where(array("url"=>$detail_url))->count()<1){
                        $con_url[$j]['url']=$detail_url;
                        $con_url[$j]['url_id']=$v['articleId'];
                        $j++;
                    }
                }
                M('Url')->addAll($con_url);
            }else{
                continue ;
            }
           
        }
        //return $con_url;
    }
    public function get_url_detail(){
        set_time_limit(0);
        ini_set('memory_limit', '2048M');
        //分类
        $type = $this->html_data('https://ai.jd.com/index_new.php?app=Discovergoods&action=getAtcListHead');
        foreach($type['indexBanner'] as $key => $item){
            if(isset($item['jump']['params']['type']) && $item['jump']['params']['type'] != 4){
                $tagtype = $item['jump']['params']['type'];
                for($i = 1; $i <= 100; $i++){//获取给分类列表链接
                    $url = "https://ai.jd.com/index_new.php?app=Discovergoods&action=getRecommendList&page={$i}&type={$tagtype}";
                    $data = $this->html_data($url);
                    if( ! empty($data['list'])){
                        $j = 0;
                        $dataAll = array();
                        $start1= microtime(true);
                        foreach($data['list'] as $k => $v){//循环类别信息
                            //获取详情链接接口
                            $detail_url = "https://ai.jd.com/index_new.php?app=Discovergoods&action=getInfoDetail&id={$v['articleId']}";
                            if(M('Url')->where(array("url"=>$detail_url))->count()<1){
                                $con_url[$j]=$detail_url;
                                $j++;
                            }
                        }
                        M('Url')->addAll($con_url);
                    }
                    continue 2;
                }
            }
        }
        //return $con_url;
    }
    //获取链接json
    private function html_data($url = '')
    {
        if(empty($url)){
            return ;
        }
        $data = $this->model_http_curl_get($url);
        $data = json_decode($data,true);
        return $data;
    }
    
    //curl 获取数据
    public function model_http_curl_get($url, $userAgent = "")
    {
        $userAgent = $userAgent ? $userAgent : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)';
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        //在访问https的时候加上下面那两行
        if(strpos($url, "https") !== false){
            date_default_timezone_set('PRC');
            curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);//终止从服务器端进行验证
        }
        curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
        $result = curl_exec($curl);
        curl_close($curl);
        
        return $result;
    }
}